package models

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

func GetOrder(OrderParma map[string]interface{}) (error, []Order, int64) {
	var ord []Order
	page := OrderParma["page"].(int)
	pageSize := OrderParma["limit"].(int)
	searchText := OrderParma["searchText"].(string)
	fmt.Println(page, pageSize, searchText)
	var total int64
	err := db.Table("orders").Offset((page-1)*pageSize).Where("oid like ?", "%"+searchText+"%").Limit(pageSize).Find(&ord).Count(&total).Error
	return err, ord, total
}

func AddOrd(c *gin.Context) (error, Order) {
	var ord Order
	err := c.ShouldBindJSON(&ord)
	if err != nil {
		fmt.Println(err.Error())
	}
	err = db.Table("orders").Select("oid", "num", "category").Create(&ord).Error
	return err, ord
}

func UpdateOrd(c *gin.Context) (error, Order) {
	var ord Order
	err := c.ShouldBindJSON(&ord)
	if err != nil {
		fmt.Println(err.Error())
	}

	err = db.Table("orders").Where("oid = ?", ord.Oid).Updates(&ord).Error
	return err, ord
}

func DelOrd(c *gin.Context) error {
	var ord Order
	err := c.ShouldBindJSON(&ord)
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(ord)
	err = db.Debug().Table("orders").Where("oid = ?", ord.Oid).Delete(&ord).Error
	return err
}
